var $$ = jQuery.noConflict();
(function ($, doc) {
    document.documentElement.style.fontSize = document.documentElement.clientWidth / 6.4 + 'px';
    $.init();
    $.ready(function () {
        //发送验证码
        $$('#phoneSend').one('touchend', function () {
            phoneSms($$('#phoneSend'))
            this.innerHTML = '60秒后重发'
            countDown($$(this))
        })
        //发送短信验证码
        function phoneSms(_this) {
            $$.post('/checkPhone', {'phone': $$('#phone').val(), 'type': 'wechat'}, function (data) {
                if (data.code == 20009) {
                    $$.post('/sendSms', {'phone': $$('#phone').val(), 'type': 'wechat'}, function (data) {
                        if (data.code != 10000) {
                            mui.alert(data.message);
                        }
                    })
                } else {
                    mui.alert(data.message);
                }
            })
        }

//60秒倒计时s
        function countDown(dom) {
            var timer, k = 60,
                i = 0
            timer = setInterval(function () {
                i++;
                dom.html((k - i) + '秒后重发')
                if ((k - i) == 0) {
                    dom.html('点击发送')
                    dom.one('touchend', function () {
                        phoneSms(dom)
                        this.innerHTML = '60秒后重发'
                        countDown($$(this))
                    })
                    clearInterval(timer)
                }
            }, 1000)
        }

        //提交
        $$('#send').on('touchend', function () {
            var i = 0;
            mui("#formData input").each(function () {
                if (!this.value || this.value.trim() == "") {
                    var label = this.previousElementSibling;
                    mui.alert(label.innerText + "不允许为空");
                    return false;
                } else {
                    i = i + 1
                }
            });
            if (i == 3) {
                if (/^([1-9]{1})(\d{15}|\d{18}|\d{16})$/.test($$('#bankcard').val())) {
                    if (/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test($$('#phone').val())) {
                        if(/^\d{6}$/.test($$('#code').val())){
                            mui(this).button('loading');
                            $$('#send').prop('disabled','true')
                            $$.post('/member/bindBank', {'bankCard':$$('#bankcard').val(),'codeParam':$$('#code').val(),'type':'wechat'}, function (res) {
                                mui($$('#send')).button('reset');
                                $$('#send').prop('disabled','false')
                                if (res.code == 10009) {
                                    mui.alert('银行卡绑定成功');
                                    openPage('/wechat/toMain')
                                } else {
                                    mui.alert(res.message);
                                }
                            })
                        }else{
                            mui.alert('验证码格式不正确');
                        }
                    } else {
                        mui.alert('手机号码不正确');
                    }
                } else {
                    mui.alert('银行卡号不正确');
                }
            }
        })
    });
})(mui, document);